package com.bj.disc.util

import com.ai3d.rfid.common.proxy.MMKVProxy
import com.bj.ski.base.common.utils.DiscConstants
import java.io.File
import kotlin.random.Random

object GameUotUtil : BaseGameUtil() {
    //根据文件路径生成所有 例如static/uot/level1
    fun generateGames(path: String): List<Game> {
        //level1
        val levelFolder = File(path)
        val themeFolders = levelFolder.listFiles()?.map { it.name }?.toMutableList()

        val games = mutableListOf<Game>()

        if (themeFolders != null && themeFolders.isNotEmpty()) {
            var themeFolders = getRandomSubset(themeFolders, 6)
            println("主题文件" + themeFolders.toString())
            for (level in 3..8) {
                var currentTheme = themeFolders[level - 3]
                val themeImageFile = File("$levelFolder/$currentTheme/1Theme.png")
                for (round in 1..4) {
                    val setFolder = File("$levelFolder/$currentTheme/Set$round")
                    val setFiles = setFolder.listFiles()

                    var correctAnswer: String
                    var incorrectAnswer: String

                    val correctIndex = Random.nextInt(setFiles.size)
                    correctAnswer = setFiles[correctIndex].path
                    val incorrectThemeIndex =
                        ((0 until themeFolders.size).filter { it != (level - 3) }).random()
                    println("currentTheme+incorrectThemeIndex" + incorrectThemeIndex)
                    val incorrectTheme = themeFolders[incorrectThemeIndex]
                    val incorrectSet = Random.nextInt(1, 5)
                    val incorrectSetFolder =
                        File("$levelFolder/$incorrectTheme/Set$incorrectSet")
                    val incorrectSetFiles = incorrectSetFolder.listFiles()
                    val incorrectIndex = Random.nextInt(incorrectSetFiles.size)
                    println("currentTheme+incorrectIndex" + incorrectIndex)
                    incorrectAnswer = incorrectSetFiles[incorrectIndex].path
//                    if(Uot?.uotTheme?.get(currentTheme)?.isNotEmpty() == true) {
//                        currentTheme = Uot.uotTheme[currentTheme].toString()
//                    }
                    games.add(
                        Game(
                            level-2,
                            round,
                            currentTheme,
                            themeImageFile.path,
                            correctAnswer,
                            setFiles[correctIndex].name,
                            incorrectAnswer,
                            incorrectSetFiles[incorrectIndex].name,
                            incorrectTheme
                        )
                    )
                }
            }
        } else {
            println("level1文件夹为空或不存在主题文件夹。")
        }

        return games
    }

    //升级难度
    fun generateHardGames(path: String): List<Game> {
        val levelFolder = File(path)
        val themeFolders =
            levelFolder.listFiles()?.filter { it.isDirectory }?.map { it.name }?.toMutableList()

        val games = mutableListOf<Game>()

        if (themeFolders != null && themeFolders.isNotEmpty()) {
            var themeFolders = getRandomSubset(themeFolders, 8)

            for (level in 3..8) {
                var currentTheme = themeFolders[level - 1]
                val themeImage = File("$levelFolder/$currentTheme/1Theme.png")
                val yesFolder = File("$levelFolder/$currentTheme")
                val yesFiles = yesFolder.listFiles { file ->
                    file.isDirectory && file.name.startsWith("yes", ignoreCase = true)
                }?.flatMap { it.listFiles()?.toMutableList() ?: emptyList() }

                val noFolder = File("$levelFolder/$currentTheme")
                val noFiles = noFolder.listFiles { file ->
                    file.isDirectory && file.name.startsWith("no", ignoreCase = true)
                }?.flatMap { it.listFiles()?.toMutableList() ?: emptyList() }

                if (yesFiles != null && noFiles != null && yesFiles.size >= 4 && noFiles.size >= 4) {
                    yesFiles.shuffled()
                    val shuffledYesFiles = getRandomFile(yesFiles, 4)
                    noFiles.shuffled()
                    val shuffledNoFiles = getRandomFile(noFiles, 4)

                    val correctAnswers = shuffledYesFiles.subList(0, 4)
                    val incorrectAnswers = shuffledNoFiles.subList(0, 4)

                    for (i in 0 until 4) {
                        val correctAnswer = correctAnswers[i].path

                        val incorrectAnswer = incorrectAnswers[i].path
//                        if(Uot?.uotTheme?.get(currentTheme)?.isNotEmpty() == true) {
//                            currentTheme = Uot.uotTheme[currentTheme].toString()
//                        }
                        games.add(
                            Game(
                                level-2,
                                i + 1,
                                currentTheme,
                                themeImage.path,
                                correctAnswer,
                                correctAnswers[i].name,
                                incorrectAnswer,
                                incorrectAnswers[i].name,
                                ""
                            )
                        )
                    }
                }
            }
        } else {
            println("level文件夹为空或不存在主题文件夹。")
        }

        return games
    }




    fun isSameLan(): Boolean {
        var same = MMKVProxy.getString(
            MMKVProxy.KEY_SETTINGS_LANGUAGE_ASSIST,
            DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US
        ).equals(
            MMKVProxy.getString(
                MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN,
                DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US
            ), true
        )
        return same
    }


}